package com.jiayi.weiphoto.activity;


import com.jiayi.weiphoto.keep.AuthoSharePreference;
import com.jiayi.weiphoto.qq.QQWeiboProxy;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

public class OAuthV1AuthorizeWebViewqq extends  Activity{
	 public final static int RESULT_CODE=0;
		private static final String TAG = "OAuthV1AuthorizeWebView";
		
		    private QQWeiboProxy mQqWeiboProxy;
		    private Context mContext;
		
		@Override
		public void onCreate(Bundle savedInstanceState)
		{ 
			mContext=this;
			super.onCreate(savedInstanceState);
			LinearLayout linearLayout=new LinearLayout(this);
			WebView webView = new WebView(this);
			linearLayout.addView(webView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			setContentView(linearLayout);
			mQqWeiboProxy = QQWeiboProxy.getInstance();
			 String urlStr = mQqWeiboProxy.getAuthoUrl();
	        WebSettings webSettings = webView.getSettings();
	        webSettings.setJavaScriptEnabled(true);
	        webSettings.setSupportZoom(true);
	        webView.requestFocus();
	        webView.loadUrl(urlStr);
	        System.out.println(urlStr.toString());
			WebViewClient client = new WebViewClient()
			{
	            @Override
	            public void onPageStarted(WebView view, String url, Bitmap favicon) {
	                Log.i(TAG, "WebView onPageStarted...");
	                Log.i(TAG, "URL = " + url);
	                if (url.indexOf("access_token=") != -1) {
	                    int start=url.indexOf("access_token=");
	                    String responseData=url.substring(start);
	                   
	                    boolean ret = mQqWeiboProxy.parseAccessTokenAndOpenId(responseData);
	                    if (ret)
	                    {
	                    	String access_token = mQqWeiboProxy.getAccessToken();
	                    	String expires_in = mQqWeiboProxy.getExpireIn();
	                    	String openID = mQqWeiboProxy.getOpenID();
	                    	String openKey = mQqWeiboProxy.getOpenKey();
	                    	 

	            			AuthoSharePreference.putToken(mContext, access_token);
	            			AuthoSharePreference.putExpires(mContext, expires_in);
	            			AuthoSharePreference.putOpenID(mContext, openID);		  
	            			AuthoSharePreference.putOpenKey(mContext, openKey);
	            			            			
	                       setResult(RESULT_OK);
	                    	finish();
	                    }else{
	                    	Toast.makeText(mContext, "Autho Fail...", Toast.LENGTH_SHORT).show();
	                    }
	              
	                    view.destroyDrawingCache();
	                    view.destroy();
	                  
	                }
	                super.onPageStarted(view, url, favicon);
	            }

			};
			webView.setWebViewClient(client);
		}
}
